package cn.uc.app;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.LinearLayout.LayoutParams;

public class MenuAdapter extends BaseAdapter {       
    private Context context;
    private MenuItemData[] datas;
    
    public MenuAdapter(Context context, MenuItemData[] aDatas) {
        this.context = context;
        datas = aDatas;
    }
    
    public void refreshData(MenuItemData[] aDatas){
        datas = aDatas;
    }
    
//  @Override
    public int getCount() {
        return datas.length;
    }

//  @Override
    public Object getItem(int arg0) {
        return datas[arg0];
    }

//  @Override
    public long getItemId(int arg0) {
        return arg0;
    }

//  @Override
    public View getView(int arg0, View arg1, ViewGroup arg2) {
        LinearLayout linear = new LinearLayout(context);
//        LinearLayout.LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        linear.setOrientation(LinearLayout.VERTICAL);
        
        ImageView iv = new ImageView(context);
        iv.setImageBitmap(((BitmapDrawable)context.getResources().getDrawable(datas[arg0].getImageId())).getBitmap());
        LinearLayout.LayoutParams params2 = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        params2.gravity=Gravity.CENTER;
        linear.addView(iv, params2);
        
        TextView tv = new TextView(context);
        tv.setText(datas[arg0].getTitle());
        LinearLayout.LayoutParams params3 = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        params3.gravity=Gravity.CENTER;
        
        linear.addView(tv, params3);
        
        return linear;
    }
}


